from django.contrib.auth.models import AbstractUser
from django.db import models

from utils.models import BaseModel


# Create your models here.
class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True, verbose_name='telephone')
    email_active = models.BooleanField(default=False, verbose_name='verify email')
    default_address = models.ForeignKey('Addresses', related_name='users', null=True,
                                        blank=True, on_delete=models.SET_NULL, verbose_name='Default Address')

    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.username


class Addresses(BaseModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='addresses', verbose_name='User Address')
    title = models.CharField(max_length=50, verbose_name='Address Title')
    receiver = models.CharField(max_length=50, verbose_name='Receiver')
    province = models.ForeignKey('area.Area', on_delete=models.PROTECT, related_name='province_addresses', verbose_name='Province Address')
    city = models.ForeignKey('area.Area', on_delete=models.PROTECT, related_name='city_addresses', verbose_name='City Address')
    district = models.ForeignKey('area.Area', on_delete=models.PROTECT, related_name='district_addresses', verbose_name='District Address')
    place = models.CharField(max_length=50, verbose_name='Detailed Address')
    mobile = models.CharField(max_length=11, verbose_name='Mobile')
    tel = models.CharField(max_length=20, null=True, blank=True, default='', verbose_name='Tel')
    email = models.CharField(max_length=30, null=True, blank=True, default='', verbose_name='Email')
    is_deleted = models.BooleanField(default=False, verbose_name='Logical Delete')

    class Meta:
        db_table = 'tb_address'
        verbose_name = 'User Address'
        verbose_name_plural = verbose_name
        ordering = ['-update_time']
